﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Actions;
using COFE.Core;
using COFE.Data.Lister;

namespace COFE.Data
{
    public class CacheListingWork : WorkBase
    {
        #region Constructor

        public CacheListingWork(Guid workId, Guid dirId, string fullParsePath, int recursionLv)
            : base(workId)
        {
            Description = "Cache_List_" + fullParsePath;
            _dirId = dirId;
            _recursionLv = recursionLv;
        }


        /// <summary>
        /// Schedule a CachePropertiesWork in COFEAction.WorkScheduler.
        /// </summary>
        /// <param name="dirId">The directory entry must already exists in database.</param>        
        /// <returns>Schedule id</returns>
        public static Guid ScheduleWork(Guid dirId, string fullParsePath, int recursionLv = 0)
        {
            var work = new CacheListingWork(Guid.NewGuid(), dirId, fullParsePath, recursionLv);
            return COFEActionShared.WorkScheduler.Enqueue(WorkPriority.Low, work);
        }

        #endregion

        #region Methods

        protected override void DoWork()
        {
            //Obtain cached version of directory entry.
            var dirEntry = COFEShared.CurrentUserProfile.TryParseGuid(_dirId) as IInternalDirectoryInfoExA;
            if (dirEntry != null)
            {
                foreach (var entry in dirEntry.CacheDirectoryLister.List(dirEntry, 
                    Core.Profile.EntryConstructionFlags.Default))
                    if (_recursionLv > 0 && entry is IDirectoryInfo)
                        CacheListingWork.ScheduleWork(entry.GetId(), entry.ParseName,  _recursionLv - 1);                        
            }            
        }

        #endregion

        #region Data

        private Guid _dirId;
        private int _recursionLv;

        #endregion 

        #region Public Properties

        #endregion
    }
}
